package edu.bupt.c2string;

public class T01AddBinary {

    public String addBinary(String a, String b) {
        int indexA = a.length() - 1;
        int indexB = b.length() -1;
        int carry = 0;

        StringBuilder res = new StringBuilder();
        while (indexA>=0 || indexB >=0){
            int valA = indexA>=0 ? a.charAt(indexA--)-'0' : 0;
            int valB = indexB>=0 ? b.charAt(indexB--)-'0' : 0;
            int temp = valA + valB + carry;
            res.insert(0,temp % 2);
            carry = temp/2;
        }

        if (1==carry){
            res.insert(0,carry);
        }

        return res.toString();
    }

    public static void main(String[] args) {
        String s = new T01AddBinary().addBinary("11", "1");
        System.out.println(s);
    }
}
